<template>
    <el-card>
      <el-row :gutter="20" class="header">
        <el-col :span="8">
          <el-input placeholder="请输入管理员账号" clearable v-model="queryForm.query" />
        </el-col>
        <el-col :span="8">
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button type="primary" :icon="Plus" @click="handleAdd">新增</el-button>
        </el-col>
      </el-row>
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="account" label="账号" width="180" />
        <el-table-column prop="name" label="姓名" width="120" />
        <el-table-column prop="mobile" label="手机号" width="120" />
        <el-table-column prop="email" label="邮箱" width="230" />
        <el-table-column prop="password" label="密码" width="200" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button
              type="danger"
              size="small"
              :icon="Delete"
              @click="handleDelete(row)"
            />
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-model:current-page="queryForm.pagenum"
        v-model:page-size="queryForm.pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <Dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      :form="currentForm"
      @success="handleSuccess"
    />
  </template>

  <script setup>
  import { ref, onMounted } from 'vue'
  import { Search, Plus, Delete } from '@element-plus/icons-vue'
  import { ElMessage, ElMessageBox } from 'element-plus'
  import Dialog from './components/dialog.vue'
  import sysAdminApi from '@/api/sys_admin'

  const loading = ref(false)
  const dialogVisible = ref(false)
  const dialogTitle = ref('')
  const currentForm = ref({
    user_id: 0,
    account: '',
    name: '',
    mobile: '',
    email: '',
    password: ''
  })
  const total = ref(0)
  const queryForm = ref({
    query: '',
    pagenum: 1,
    pageSize: 10
  })

  const tableData = ref([])

  // 获取列表数据
  const getList = async () => {
    try {
      loading.value = true
      const { data } = await sysAdminApi.getAdmin(queryForm.value)
      if (data) {
        if (Array.isArray(data)) {
          tableData.value = data
          total.value = data.length
        } else {
          ElMessage.error('获取管理员列表失败')
        }
      } else {
        ElMessage.error('获取管理员列表失败')
      }
    } catch (error) {
      console.error('获取列表失败:', error)
      ElMessage.error('获取列表失败')
    } finally {
      loading.value = false
    }
  }

  // 搜索
  const handleSearch = () => {
    queryForm.value.pagenum = 1
    getList()
  }

  // 新增
  const handleAdd = () => {
    currentForm.value = {
      user_id: 0,
      account: '',
      name: '',
      mobile: '',
      email: '',
      role: 'admin',
      status: 1,
      password: ''
    }
    dialogTitle.value = '新增管理员'
    dialogVisible.value = true
  }

  // 删除
  const handleDelete = async (row) => {
    if (row.role === 'super') {
      ElMessage.warning('超级管理员不能删除')
      return
    }
    try {
      await ElMessageBox.confirm('确认删除该管理员?', '提示', {
        type: 'warning'
      })
      const { data } = await sysAdminApi.deleteAdmin(row.user_id)
      if (data) {
        ElMessage.success('删除成功')
        getList()
      }
    } catch (error) {
      console.error('删除失败:', error)
      ElMessage.error('删除失败')
    }
  }

  // 分页
  const handleSizeChange = (size) => {
    queryForm.value.pagenum = 1
    queryForm.value.pageSize = size
    getList()
  }

  const handleCurrentChange = (page) => {
    queryForm.value.pagenum = page
    getList()
  }

  // 表单提交成功
  const handleSuccess = () => {
    getList()
  }

  onMounted(() => {
    getList()
  })
  </script>

  <style lang="scss" scoped>
  .header {
    padding: 16px;
    box-sizing: border-box;
  }

  :deep(.el-pagination) {
    margin-top: 16px;
    justify-content: center;
  }

  .el-tag {
    margin: 0 4px;
    &:first-child {
      margin-left: 0;
    }
  }

  :deep(.el-table) {
    .cell {
      white-space: nowrap;
    }
  }
  </style>
